app.controller("homeCtrl", ['$scope', '$window', '$http', '$cookieStore', '$state', '$filter', function ($scope, $window, $http, $cookieStore, $state, $filter) {
    $window.document.title = '共享公寓'
    // 判断登录函数
    $scope.login();
    $(function () {
        var lap
        var map, geolocation;
        //加载地图，调用浏览器定位服务
        map = new AMap.Map('container', {
            resizeEnable: true
        });
        map.plugin('AMap.Geolocation', function () {
            geolocation = new AMap.Geolocation({
                enableHighAccuracy: true,//是否使用高精度定位，默认:true
                timeout: 10000,          //超过10秒后停止定位，默认：无穷大
                buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
                zoomToAccuracy: true,      //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
                buttonPosition: 'RB'
            });
            map.addControl(geolocation);
            geolocation.getCurrentPosition();
            AMap.event.addListener(geolocation, 'complete', function (data) {
                // 把经纬度存入cookies

                // $cookieStore.put('cityName',{id:$scope.showCity.id,name:$scope.showCity.name});
                $cookieStore.put('lng&lat', { lng: data.position.getLng(), lat: data.position.getLat() })

                //获取cookies                
                if ($cookieStore.get('cityName')) {
                    console.log($cookieStore.get('cityName'))
                    // 长租数据
                    var data1 = {
                        id: $cookieStore.get('cityName').id,
                        style: 1,
                        count: 5,
                        order: 2
                    }
                    // 民宿数据
                    var data2 = {
                        id: $cookieStore.get('cityName').id,
                        style: 2,
                        count: 5,
                        order: 2
                    }
                    // 营业房数据
                    var data3 = {
                        id: $cookieStore.get('cityName').id,
                        style: 3,
                        count: 5,
                        order: 2
                    }
                    // 出租数据
                    var data4 = {
                        id: $cookieStore.get('cityName').id,
                        style: 4,
                        count: 5,
                        order: 2
                    }
                } else {
                    // 长租数据
                    var data1 = {
                        lng: data.position.getLng(),
                        lat: data.position.getLat(),
                        style: 1,
                        count: 5
                    }
                    // 民宿数据
                    var data2 = {
                        lng: data.position.getLng(),
                        lat: data.position.getLat(),
                        style: 2,
                        count: 5
                    }
                    // 营业房数据
                    var data3 = {
                        lng: data.position.getLng(),
                        lat: data.position.getLat(),
                        style: 3,
                        count: 5
                    }
                    // 出租数据
                    var data4 = {
                        lng: data.position.getLng(),
                        lat: data.position.getLat(),
                        style: 4,
                        count: 5
                    }
                }

                // 地址和banner
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/index/index.html',
                    data: {
                        do: 'index'
                        // openID:'ksksk123456',
                        // lng:data.position.getLng(),
                        // lat:data.position.getLat()              
                    },
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {
                    $scope.names = response.data;
                    $scope.city = response.data.data.area;//首次请求地址数据
                    $scope.bannerImg = $scope.httpSrc + response.data.data.img;//首次请求bannerImg
                }, function errorCallback(response) {
                    console.log(response)
                    // 请求失败执行代码
                });
                // 请求房源推荐数据长租
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/house/lst.html',
                    data: data1,
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {
                    $scope.longData = response.data.data;
                    console.log($scope.longData)

                }, function errorCallback(response) {
                    console.log(response)
                    // 请求失败执行代码
                });
                // 请求房源推荐数据民宿
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/house/lst.html',
                    data: data2,
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {
                    $scope.PensionData = response.data.data;
                    console.log($scope.PensionData)

                }, function errorCallback(response) {
                    console.log(response)
                    // 请求失败执行代码
                });
                // 请求房源推荐数据营业房租贷
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/house/lst.html',
                    data: data3,
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {
                    $scope.RentData = response.data.data;
                    console.log($scope.longData)

                }, function errorCallback(response) {
                    console.log(response)
                    // 请求失败执行代码
                });
                // 请求房源推荐数据住房出租
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/house/lst.html',
                    data: data4,
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {
                    $scope.businessData = response.data.data;
                    console.log($scope.longData)
                    // alert(response.data)    

                }, function errorCallback(response) {
                    console.log(response)
                    // 请求失败执行代码
                });


            });//返回定位信息









            AMap.event.addListener(geolocation, 'error', function (data) {
                // 模拟定位




                $cookieStore.put('lng&lat', { lng: 104.323665, lat: 31.2222222 })
                if ($cookieStore.get('cityName')) {
                    console.log($cookieStore.get('cityName'))
                    // 长租数据
                    var data1 = {
                        id: $cookieStore.get('cityName').id,
                        style: 1,
                        count: 5,
                        order: 2
                    }
                    // 民宿数据
                    var data2 = {
                        id: $cookieStore.get('cityName').id,
                        style: 2,
                        count: 5,
                        order: 2
                    }
                    // 营业房数据
                    var data3 = {
                        id: $cookieStore.get('cityName').id,
                        style: 3,
                        count: 5,
                        order: 2
                    }
                    // 出租数据
                    var data4 = {
                        id: $cookieStore.get('cityName').id,
                        style: 4,
                        count: 5,
                        order: 2
                    }
                } else {
                    // 长租数据
                    var data1 = {
                        lng: 123.111111,
                        lat: 31.22222,
                        style: 1,
                        count: 5

                    }
                    // 民宿数据
                    var data2 = {
                        lng: 123.111111,
                        lat: 31.22222,
                        style: 2,
                        count: 5
                    }
                    // 营业房数据
                    var data3 = {
                        lng: 123.111111,
                        lat: 31.22222,
                        style: 3,
                        count: 5
                    }
                    // 出租数据
                    var data4 = {
                        lng: 123.111111,
                        lat: 31.22222,
                        style: 4,
                        count: 5
                    }
                }



                // 地址和banner
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/index/index.html',
                    data: {
                        do: 'index',

                    },
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {
                    $scope.names = response.data;
                    $scope.city = response.data.data.area;//首次请求地址数据
                    $scope.bannerImg = $scope.httpSrc + response.data.data.img;//首次请求bannerImg
                    console.log(response.data.data.img)
                }, function errorCallback(response) {
                    console.log(response)
                    // 请求失败执行代码
                });
                // 请求房源推荐数据长租
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/house/lst.html',
                    data: data1,
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {
                    $scope.longData = response.data.data;
                    console.log($scope.longData)
                }, function errorCallback(response) {
                    console.log(response)
                    // 请求失败执行代码
                });
                // 请求房源推荐数据民宿
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/house/lst.html',
                    data: data2,
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {
                    $scope.PensionData = response.data.data;
                    console.log($scope.PensionData)

                }, function errorCallback(response) {
                    console.log(response)
                    // 请求失败执行代码
                });
                // 请求房源推荐数据营业房租贷
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/house/lst.html',
                    data: data3,
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: $scope.changeData
                }).then(function successCallback(response) {
                    $scope.RentData = response.data.data;
                    console.log($scope.longData)

                }, function errorCallback(response) {
                    console.log(response)
                    // 请求失败执行代码
                });
                // 请求房源推荐数据住房出租
                $http({
                    method: 'post',
                    url: $scope.httptit + '/index/house/lst.html',
                    data: data4,
                    dataType: 'json',
                    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                    transformRequest: function (obj) {
                        var str = [];
                        for (var p in obj) {
                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                        }
                        return str.join("&");
                    }
                }).then(function successCallback(response) {
                    $scope.businessData = response.data.data;
                    console.log($scope.longData)

                }, function errorCallback(response) {
                    console.log(response)
                    // 请求失败执行代码
                });





            });      //返回定位出错信息
        });
        // 首页banner高度
        $('.BannerImg').height($window.screen.width / 1.63);

    })

    // 点击选择城市定位
    $scope.showCity = $cookieStore.get('cityName');
    $scope.collAddress = function (v, index) {
        $http({
            method: 'post',
            url: $scope.httptit + '/index/index/index.html',
            data: {
                id: v,
                do: 'city'
            },
            dataType: 'json',
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
            transformRequest: $scope.changeData
        }).then(function successCallback(response) {
            $scope.names = response.data;
            if (response.data.status == 102) {
                $scope.showCity = $scope.city[index];
                $cookieStore.put('cityName', { id: $scope.showCity.id, name: $scope.showCity.name });
                $scope.local = !$scope.local;
                history.go(0);
                return
            } else {
                $scope.city = response.data.data
            }
        }, function errorCallback(response) {
            console.log(response)
            // 请求失败执行代码
        });
    }
    // 重新点击获取城市数据
    $scope.cityColl = function () {
        $scope.local = !$scope.local
        if (!$scope.local) {
            // 模拟定位
            $http({
                method: 'post',
                url: $scope.httptit + '/index/index/index.html',
                data: {
                    do: 'index'
                },
                dataType: 'json',
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                transformRequest: $scope.changeData
            }).then(function successCallback(response) {
                $scope.names = response.data;
                console.log(response.data)
                $scope.city = response.data.data.area;

            }, function errorCallback(response) {
                console.log(response)
                // 请求失败执行代码
            });
            // alert('定位失败，请刷新后重试')
        }

    }






}]);
